/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImagePanel.java
 *
 * Created on Apr 30, 2012, 11:06:55 AM
 */
package org.biz.invoicesystem.master.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

/**
 *
 * @author d
 */
public class ImagePanel extends javax.swing.JPanel {

    private double m_zoom = 1.0;
    private double m_zoomPercentage;
    private Image m_image;

    /** 
     * Constructor 
     *  
     * @param image 
     * @param zoomPercentage 
     */
    public ImagePanel(Image image, double zoomPercentage) {
        m_image = image;
        m_zoomPercentage = zoomPercentage / 100;
    }

    /** 
     * This method is overriden to draw the image 
     * and scale the graphics accordingly 
     */
    public void paintComponent(Graphics grp) {
        Graphics2D g2D = (Graphics2D) grp;

        //set the background color to white 
        g2D.setColor(Color.WHITE);
        //fill the rect 
        g2D.fillRect(0, 0, getWidth(), getHeight());

        //scale the graphics to get the zoom effect 
        g2D.scale(m_zoom, m_zoom);

        //draw the image 
        g2D.drawImage(m_image, 0, 0, this);
    }

    /** 
     * This method is overriden to return the preferred size 
     * which will be the width and height of the image plus 
     * the zoomed width width and height.  
     * while zooming out the zoomed width and height is negative 
     */
//        public Dimension getPreferredSize() 
//        { 
//            return new Dimension((int)(m_image.getWidth(this) +  
//                                      (m_image.getWidth(this) * (m_zoom - 1))), 
//                                 (int)(m_image.getHeight(this) +  
//                                      (m_image.getHeight(this) * (m_zoom -1 )))); 
//        } 
    public Dimension getPreferredSize() {
        return new Dimension(250, 250);
    }

    /** 
     * Sets the new zoomed percentage 
     * @param zoomPercentage 
     */
    public void setZoomPercentage(int zoomPercentage) {
        m_zoomPercentage = ((double) zoomPercentage) / 100;
    }

    /** 
     * This method set the image to the original size 
     * by setting the zoom factor to 1. i.e. 100% 
     */
    public void originalSize() {
        m_zoom = 1;
    }

    /** 
     * This method increments the zoom factor with 
     * the zoom percentage, to create the zoom in effect  
     */
    public void zoomIn() {
        m_zoom += m_zoomPercentage;
    }

    /** 
     * This method decrements the zoom factor with the  
     * zoom percentage, to create the zoom out effect  
     */
    public void zoomOut() {
        m_zoom -= m_zoomPercentage;

        if (m_zoom < m_zoomPercentage) {
            if (m_zoomPercentage > 1.0) {
                m_zoom = 1.0;
            } else {
                zoomIn();
            }
        }
    }

    /** 
     * This method returns the currently 
     * zoomed percentage 
     *  
     * @return 
     */
    public double getZoomedTo() {
        return m_zoom * 100;
    }

    /** Creates new form ImagePanel */
    public ImagePanel() {
//        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
